package com.qiyi.player.wonder.plugins.share.view
{
   import com.iqiyi.components.panelSystem.impls.BasePanel;
   import com.qiyi.player.wonder.common.status.Status;
   import com.qiyi.player.wonder.common.vo.UserInfoVO;
   import com.qiyi.player.wonder.plugins.share.view.parts.VideoShare;
   import com.qiyi.player.base.pub.EnumItem;
   import com.qiyi.player.core.model.def.PlayerTypeEnum;
   import com.qiyi.player.wonder.plugins.share.view.parts.VideoShareEvent;
   import com.qiyi.player.wonder.plugins.share.ShareDef;
   import flash.display.DisplayObjectContainer;
   import com.iqiyi.components.global.GlobalStage;
   import gs.TweenLite;
   import com.qiyi.player.wonder.body.BodyDef;
   import flash.events.Event;
   import com.qiyi.player.wonder.common.pingback.PingBack;
   
   public class ShareView extends BasePanel
   {
      
      public function ShareView(param1:DisplayObjectContainer, param2:Status, param3:UserInfoVO) {
         super(NAME,param1);
         type = BodyDef.VIEW_TYPE_POPUP;
         this._status = param2;
         this._userInfoVO = param3;
      }
      
      public static const NAME:String = "com.qiyi.player.wonder.plugins.share.view.ShareView";
      
      private var _status:Status;
      
      private var _userInfoVO:UserInfoVO;
      
      private var _videoShareView:VideoShare;
      
      public function updateOpenParam(param1:Boolean, param2:int, param3:EnumItem, param4:String, param5:String, param6:String, param7:String, param8:String, param9:String) : void {
         this.destroyVideoShareView();
         this._videoShareView = new VideoShare(PlayerTypeEnum.MAIN_STATION,false,param1,param2,param3,param4,param5,param6,param7,param8,param9);
         this._videoShareView.addEventListener(VideoShareEvent.Evt_Unfold,this.onUnFold);
         this._videoShareView.addEventListener(VideoShareEvent.Evt_PackUp,this.onPackUp);
         this._videoShareView.addEventListener(VideoShareEvent.Evt_Close,this.onVideoShareViewClose);
         this._videoShareView.addEventListener(VideoShareEvent.Evt_StartShare,this.onVideoStartShare);
         addChild(this._videoShareView);
      }
      
      public function onUserInfoChanged(param1:UserInfoVO) : void {
         this._userInfoVO = param1;
      }
      
      public function onAddStatus(param1:int) : void {
         this._status.addStatus(param1);
         switch(param1)
         {
            case ShareDef.STATUS_OPEN:
               this.open();
               break;
         }
      }
      
      public function onRemoveStatus(param1:int) : void {
         this._status.removeStatus(param1);
         switch(param1)
         {
            case ShareDef.STATUS_OPEN:
               this.close();
               break;
         }
      }
      
      public function onResize(param1:int, param2:int) : void {
         if(isOnStage)
         {
            x = (param1 - this._videoShareView.getWidth()) / 2;
            y = (param2 - this._videoShareView.getHeight()) / 2;
         }
      }
      
      override public function open(param1:DisplayObjectContainer = null) : void {
         if(!isOnStage)
         {
            super.open(param1);
            dispatchEvent(new ShareEvent(ShareEvent.Evt_Open));
         }
      }
      
      override public function close() : void {
         if(isOnStage)
         {
            super.close();
            dispatchEvent(new ShareEvent(ShareEvent.Evt_Close));
         }
      }
      
      override protected function onAddToStage() : void {
         super.onAddToStage();
         this.onResize(GlobalStage.stage.stageWidth,GlobalStage.stage.stageHeight);
         alpha = 0;
         TweenLite.to(this,BodyDef.POPUP_TWEEN_TIME / 1000,{"alpha":1});
      }
      
      override protected function onRemoveFromStage() : void {
         super.onRemoveFromStage();
         TweenLite.killTweensOf(this);
         this.destroyVideoShareView();
      }
      
      private function destroyVideoShareView() : void {
         if(this._videoShareView)
         {
            this._videoShareView.removeEventListener(VideoShareEvent.Evt_Unfold,this.onUnFold);
            this._videoShareView.removeEventListener(VideoShareEvent.Evt_PackUp,this.onPackUp);
            this._videoShareView.removeEventListener(VideoShareEvent.Evt_Close,this.onVideoShareViewClose);
            this._videoShareView.removeEventListener(VideoShareEvent.Evt_StartShare,this.onVideoStartShare);
            removeChild(this._videoShareView);
            this._videoShareView.destory();
            this._videoShareView = null;
         }
      }
      
      private function onUnFold(param1:Event) : void {
         this.onResize(GlobalStage.stage.stageWidth,GlobalStage.stage.stageHeight);
         dispatchEvent(new ShareEvent(ShareEvent.Evt_UnfoldChanged,true));
      }
      
      private function onPackUp(param1:Event) : void {
         this.onResize(GlobalStage.stage.stageWidth,GlobalStage.stage.stageHeight);
         dispatchEvent(new ShareEvent(ShareEvent.Evt_UnfoldChanged,false));
      }
      
      private function onVideoShareViewClose(param1:Event) : void {
         this.close();
      }
      
      private function onVideoStartShare(param1:VideoShareEvent) : void {
         PingBack.getInstance().videoSharePing(param1.shareType,param1.shareStartTime,param1.shareEndTime);
         dispatchEvent(new ShareEvent(ShareEvent.Evt_ShareBtnClick));
      }
   }
}
